package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.exceptions.BusinessException;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author W.J.Y
 */
@RestController
@RequestMapping("orderSetting")
public class OrderSettingController {


    @Reference(version = "1.0.0")
    private OrderSettingService orderSettingService;

    /**
     * 预约设置文件上传
     * @param excelFile 预约设置文件
     * @return  返回消息
     */
    @RequestMapping("upload")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){

        try {
            List<String[]> strings = POIUtils.readExcel(excelFile);
            List<OrderSetting> orderSettings = new ArrayList<>();
            for (String[] string : strings) {
                //预约日期
                Date orderDate = new SimpleDateFormat("yyyy/MM/dd").parse(string[0]);
                //可预约人数
                int number = Integer.parseInt(string[1]);
                //已预约人数
                int reservations =0;
                OrderSetting orderSetting = new OrderSetting(orderDate,number,reservations);
                orderSettings.add(orderSetting);
            }
            orderSettingService.add(orderSettings);
            //导入成功返回结果
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException | ParseException e) {
            e.printStackTrace();
            throw new BusinessException("上传文件解析错误，请检查文件内容");
        }
    }

    /**
     * 根据日期获取当月预约设置信息
     * @param dateStr  日期
     * @return  返回json结果
     */
    @RequestMapping("findByDate/{dateStr}")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result findByDate(@PathVariable String dateStr){

        List<Map> orderSettings = orderSettingService.findByDateInterval(dateStr);

        if(orderSettings==null||orderSettings.size()==0){
            throw new BusinessException("本月无数据！");
        }

        return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,orderSettings);
    }

    /**
     * 编辑预约设置
     * @param orderSetting  预约设置实例
     * @return  返回结果
     */
    @RequestMapping("update")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result update(@RequestBody OrderSetting orderSetting) throws ParseException {

        orderSettingService.update(orderSetting);

        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }
}
